package com.sjzsqjy.asdtiang.action;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.sjzsqjy.asdtiang.db.SearchService;
import com.sjzsqjy.asdtiang.mysql.Doctor;
import com.sjzsqjy.asdtiang.mysql.ItemSet;
import com.sjzsqjy.asdtiang.mysql.ShiQiaoResult;
import com.sjzsqjy.asdtiang.mysql.ShiQiaoUser;
import com.sjzsqjy.asdtiang.search.ShiQiaoResultFormat;

public class ShiQiaoSearchAction extends ActionSupport {
	private Log log = LogFactory.getLog(ShiQiaoSearchAction.class);
	private List result;
	private Doctor doctor;
	private ShiQiaoUser shiQiaoUser;
	private SearchService searchService;
	private String photo;
	private boolean showPhoto;
	private String  date;
	private String  sampleId;

	public String getDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = date;
	}

	public String getSampleId() {
		return sampleId;
	}

	public void setSampleId(String sampleId) {
		this.sampleId = sampleId;
	}

	public boolean isShowPhoto() {
		return showPhoto;
	}

	public void setShowPhoto(boolean showPhoto) {
		this.showPhoto = showPhoto;
	}

	public String execute() throws Exception {
		List l;
		shiQiaoUser = (ShiQiaoUser) ActionContext.getContext().getSession()
				.get("shiQiaoUser");
		if (null == shiQiaoUser) {
			log.info("date"+date+"dampleId:"+sampleId);
			shiQiaoUser=searchService.shiQiaoUserSearch1(date, sampleId);
			l = searchService.shiQiaoResult(date,sampleId);
		} else {
			if (searchService == null) {
				log.info("service is null");
			}
			log.info(shiQiaoUser.getId().getDate());
			l = searchService.shiQiaoResult(shiQiaoUser.getId().getDate(),
					shiQiaoUser.getId().getSampleId());
		}
		doctor=searchService.doctor(shiQiaoUser.getDoctorId());
		result = new ArrayList();
		ShiQiaoResultFormat r = null;
		ShiQiaoResult s = null;
		ItemSet itemSet = null;
		for (int i = 0; i < l.size(); i++) {
			s = (ShiQiaoResult) l.get(i);
			r = new ShiQiaoResultFormat();
			r.setCode(s.getItemCode());
			itemSet = searchService.itemSet(r.getCode());
			if (null != itemSet) {
				r.setName(itemSet.getName());
				r.setUnit(itemSet.getUnit());
				log.info("charValue:"+s.getCharValue()+"::");
				if (s.getCharValue().equals(" ")) {
					r.setValue(s.getValue());
					log.info("value:"+s.getValue());
					log.info("hight:"+itemSet.getHight());
					r.setLow(Float.parseFloat(itemSet.getLow()));
					r.setHight(Float.parseFloat(itemSet.getHight()));
					r.setSequence(itemSet.getSequence());
					r.normal();
				} else {
					r.setValue(s.getCharValue());
				}
				if (r.getCode().equals("RBC")
						&& shiQiaoUser.getSendDoctor().equals("女")) {
					r.setHight(5);
				}
				if (r.getCode().equals("HGB")
						&& shiQiaoUser.getSendDoctor().equals("女")) {
					r.setHight(160);
				}

				if (r.getCode().equals("CRBC") || r.getCode().equals("CWBC")
						|| r.getCode().equals("CPLT")) {
					// //为这三项时不用添加到显示
				} else {
					result.add(r);
				}
			}

		}
		Collections.sort(result, new Comparator() {
			public int compare(Object o1, Object o2) {
				// TODO Auto-generated method stub
				return ((ShiQiaoResultFormat) o1).getSequence()
						- ((ShiQiaoResultFormat) o2).getSequence();
			}
		});
		if (result.size() > 15) {
			showPhoto = true;
		}
		
		photo = formatDate(shiQiaoUser.getId().getDate());
		return SUCCESS;
	}

	public List getResult() {
		return result;
	}

	public void setResult(List result) {
		this.result = result;
	}

	public Doctor getDoctor() {
		return doctor;
	}

	public void setDoctor(Doctor doctor) {
		this.doctor = doctor;
	}

	public ShiQiaoUser getShiQiaoUser() {
		return shiQiaoUser;
	}

	public void setShiQiaoUser(ShiQiaoUser shiQiaoUser) {
		this.shiQiaoUser = shiQiaoUser;
	}

	public SearchService getSearchService() {
		return searchService;
	}

	public void setSearchService(SearchService searchService) {
		this.searchService = searchService;
	}

	public String getPhoto() {
		return photo;
	}

	public void setPhoto(String photo) {
		this.photo = photo;
	}

	 /**
	   *
	   * @param 2010-05-08
	   * @return 2010-5-8
	   */
	public static String formatDate(String date) {
		String[] str = date.split("-");
		return str[0] + "-" + Integer.parseInt(str[1]) + "-" + Integer.parseInt(str[2]);
	}

}
